﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Linq.Expressions;
using System.Reflection;

namespace DevFx.Web.Code
{
    public class linqtext
    {
         public static IQueryable<T> DataSorting<T>(IQueryable<T> source, string sortExpression, string sortDirection)
         {
             string sortingDir = string.Empty;
             if (sortDirection.ToUpper().Trim() == "ASC")
                 sortingDir = "OrderBy";
             else if (sortDirection.ToUpper().Trim() == "DESC")
                 sortingDir = "OrderByDescending";
             ParameterExpression param = Expression.Parameter(typeof(T), sortExpression);
             PropertyInfo pi = typeof(T).GetProperty(sortExpression);
             Type[] types = new Type[2];
             types[0] = typeof(T);
             types[1] = pi.PropertyType;
             Expression expr = Expression.Call(typeof(Queryable), sortingDir, types, source.Expression, Expression.Lambda(Expression.Property(param, sortExpression), param));
             IQueryable<T> query = source.AsQueryable().Provider.CreateQuery<T>(expr);
             return query;
         }
 
         public static IQueryable<T> DataPaging<T>(IQueryable<T> source, int pageNumber, int pageSize)
         {
             return source.Skip(pageNumber * pageSize).Take(pageSize);
         }
 
         public static IQueryable<T> SortingAndPaging<T>(IQueryable<T> source, string sortExpression, string sortDirection, int pageNumber, int pageSize)
         {
             IQueryable<T> query = DataSorting<T>(source, sortExpression, sortDirection);
             return DataPaging(query, pageNumber, pageSize);
         }
     }
    
}